﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;

namespace YJKFamilyComparison
{
	internal class DocumentResultPacker : ResultPackerBase
	{
		public DocumentResultPacker(bool isLinkDocument, DocumentResultPacker parentDocumentPacker, Autodesk.Revit.DB.Document document)
		{
			this.categoryFilter = new CategoryFilter();
			this.UseId = document.PathName.ToString().GetHashCode();
			this.UseName = document.PathName;
			this.NodeName = document.PathName;
			this.isLinkDocument = isLinkDocument;
			this.parentDocumentPacker = parentDocumentPacker;
			this.thisDocument = document;
			this.thisPath = this.thisDocument.PathName;
			this.lstCatagoryResults = new List<CategoryResultPacker>();
			this.childPackers = new ChildResultPackerDataStructure();
			this.lstLinkDocumentResults = new List<DocumentResultPacker>();
		}

		public bool IsLinkDocument
		{
			get
			{
				return this.isLinkDocument;
			}
			set
			{
				this.isLinkDocument = value;
			}
		}

		public DocumentResultPacker ParentDocumentPacker
		{
			get
			{
				return this.parentDocumentPacker;
			}
			set
			{
				this.parentDocumentPacker = value;
			}
		}

		public Document ThisDocument
		{
			get
			{
				return this.thisDocument;
			}
		}

		public string ThisPath
		{
			get
			{
				return this.thisPath;
			}
		}

		public List<CategoryResultPacker> LstCatagoryResults
		{
			get
			{
				return this.lstCatagoryResults;
			}
			set
			{
				this.lstCatagoryResults = value;
			}
		}

		public List<DocumentResultPacker> LstLinkDocumentResults
		{
			get
			{
				return this.lstLinkDocumentResults;
			}
			set
			{
				this.lstLinkDocumentResults = value;
			}
		}

		public void OrderLnkDocumentList()
		{
			this.LstCatagoryResults = (from k in this.LstCatagoryResults
			orderby k.Name
			select k).ToList<CategoryResultPacker>();
		}

		public void MakeResultList()
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.thisDocument);
			ICollection<Element> collection = new List<Element>();
			List<int> list = new List<int>();
			foreach (Element element in filteredElementCollector.WhereElementIsNotElementType().ToElements())
			{
				list.Add(element.GetTypeId().IntegerValue);
			}
			list = list.Distinct<int>().ToList<int>();
			foreach (int num in list)
			{
				collection.Add(this.thisDocument.GetElement(new ElementId(num)));
			}
			new FilteredElementCollector(this.thisDocument);
			foreach (Element element2 in collection)
			{
				Autodesk.Revit.DB.ElementType elementType = element2 as Autodesk.Revit.DB.ElementType;
				try
				{
					if (elementType.Category != null)
					{
						if (this.categoryFilter.IfCategoryUse(elementType.Category))
						{
							ElementTypeResultPacker elementTypeResultPacker = new ElementTypeResultPacker(elementType);
							elementTypeResultPacker.MakeParameters();
							elementTypeResultPacker.Order();
							KeyValuePair<int, int> categoryIndex = Utility.GetCategoryIndex(this.childPackers, elementType.Category);
							if (-999 != categoryIndex.Key && -999 != categoryIndex.Value)
							{
								KeyValuePair<int, int> familyNameIndex = Utility.GetFamilyNameIndex(this.childPackers.ThisValue.ElementAt(categoryIndex.Key).Value[categoryIndex.Value].ChildPackers, elementType.FamilyName);
								if (-999 != familyNameIndex.Key && -999 != familyNameIndex.Value)
								{
									this.childPackers.ThisValue.ElementAt(categoryIndex.Key).Value[categoryIndex.Value].ChildPackers.ThisValue.ElementAt(familyNameIndex.Key).Value[familyNameIndex.Value].ChildPackers.AddPacker(elementTypeResultPacker);
								}
								else
								{
									FamilyNameResultPacker familyNameResultPacker = new FamilyNameResultPacker(elementType.FamilyName);
									ChildResultPackerDataStructure childResultPackerDataStructure = new ChildResultPackerDataStructure();
									childResultPackerDataStructure.AddPacker(elementTypeResultPacker);
									familyNameResultPacker.SetChildResultPackerDataStructure(childResultPackerDataStructure);
									this.childPackers.ThisValue.ElementAt(categoryIndex.Key).Value[categoryIndex.Value].ChildPackers.AddPacker(familyNameResultPacker);
								}
							}
							else
							{
								CategoryResultPacker categoryResultPacker = new CategoryResultPacker(elementType.Category);
								FamilyNameResultPacker familyNameResultPacker2 = new FamilyNameResultPacker(elementType.FamilyName);
								ChildResultPackerDataStructure childResultPackerDataStructure2 = new ChildResultPackerDataStructure();
								ChildResultPackerDataStructure childResultPackerDataStructure3 = new ChildResultPackerDataStructure();
								childResultPackerDataStructure3.AddPacker(elementTypeResultPacker);
								familyNameResultPacker2.SetChildResultPackerDataStructure(childResultPackerDataStructure3);
								childResultPackerDataStructure2.AddPacker(familyNameResultPacker2);
								categoryResultPacker.SetChildResultPackerDataStructure(childResultPackerDataStructure2);
								this.childPackers.AddPacker(categoryResultPacker);
							}
						}
					}
				}
				catch (Exception)
				{
				}
			}
		}

		public ICollection<Element> filtrateElementType(ICollection<Element> elementtypes, List<string> conditons)
		{
			return null;
		}

		private CategoryFilter categoryFilter;

		private bool isLinkDocument;

		private DocumentResultPacker parentDocumentPacker;

		private Document thisDocument;

		private string thisPath;

		private List<CategoryResultPacker> lstCatagoryResults;

		private List<DocumentResultPacker> lstLinkDocumentResults;
	}
}
